<?php

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller;

use Imi\Aop\Annotation\Inject;
use Psr\Http\Message\ResponseInterface;

// use ImiApp\ApiServer\Common\AuthApi;
use Phpben\Imi\Auth\Annotation\Auth;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Route;
use Imi\Server\Http\Route\Annotation\Controller;
use Phpben\Imi\Validate\Annotation\Validate;
use Imi\Db\Db;


/**
 * API公共控制器
 * @Auth(name="token",nologin={"get_config","get_storebulletin_list","get_storebulletin_data","get_error_code"})
 * @Controller("/com/")
 */
class Com extends ApiBase
{


    /**
     * 获取配置
     *
     * @Action
     * @param $field
     * @return void
     */
    public function get_config($field = null)
    {
        if ($field) {
            return $this->success($field, config($field));
        } else {
            return $this->success('all', config());
        }
    }


    /**
     * 获取店铺发布的公告列表🐲
     *
     * @param int $store_id 店铺id
     * @param integer $page 1
     * @param integer $limit 8
     * @Action
     * @return void
     */
    public function get_storebulletin_list(?int $store_id = null,?int $page = 1,?int $limit = 8)
    {
        $data['status'] = '1';
        if ($store_id)
            $data['store_id'] = $store_id;
        $list = Db::name('storebulletin')->whereEx($data)->orderRaw('createtime desc')->page($page, $limit)->select()->getArray();
        foreach ($list as $key => &$val) {
            $val['date'] = date('Y-m-d H:i', $val['createtime']);
        }
        return $this->success('ok', $list);
    }

    /**
     * 获取店铺发布的公告详情🐲
     *
     * @param int $id 公告id
     * @Action
     * @return void
     */
    public function get_storebulletin_data(?int $id = null)
    {
        $data['status'] = '1';
        $data['id'] = $id;
        $res = Db::name('storebulletin')->whereEx($data)->find();
        if (!$res) {
            return $this->error('没有该信息');
        }
        $res['date'] = date('Y-m-d H:i', $res['createtime']);
        return $this->success('ok', $res);
    }



    /**
     * 获取快递公司列表
     *
     * @param string $name 搜索名
     * @param int $page 1
     * @param int $limit 10
     * @return void
     * @Action
     */
    public function get_express($name = null, ?int $page = 1, ?int $limit = 10)
    {
        $page = $page ?: 1;
        $limit = $limit ?: 10;
        $where = [];
        if ($name)
            $where['name'] = ['like', '%' . $name . '%'];
        $list = Db::name('express')->whereEx($where)->page($page, $limit)->select()->getArray();
        return $this->success('list', $list);
    }

    /**
     * 获取错误代码
     *
     * @param int $code 错误代码
     * @Action
     * @return void
     */
    public function get_error_code($code = null)
    {
        $arr = get_defined_constants(true)['user'];
        $new = [];
        foreach ($arr as $key => $val) {
            if (substr($key, 0, 5) == 'CODE_') {
                $new[$key] = $val;
            }
        }
        return $this->success('代码', $new);
    }
}
